{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "def draw_sin_wave(width=80, height=20):\n",
    "    # 创建一个空的二维数组来表示画布\n",
    "    canvas = [[' ' for _ in range(width)] for _ in range(height)]\n",
    "    \n",
    "    # 准备画图\n",
    "    for x in range(width):\n",
    "        # 计算对应的x值在 [-pi, pi] 范围内\n",
    "        angle = (x / width) * (2 * math.pi) - math.pi\n",
    "        # 计算sin值并将其映射到画布的纵坐标\n",
    "        y = math.sin(angle)\n",
    "        # 将sin值映射到画布的纵坐标范围 [0, height-1]\n",
    "        canvas_y = int((y + 1) * (height // 2))  # 映射到 [0, height-1]\n",
    "        \n",
    "        # 确保 canvas_y 在有效范围内\n",
    "        canvas_y = max(0, min(height - 1, canvas_y))\n",
    "        \n",
    "        # 在画布上对应的位置放置 '*'\n",
    "        canvas[canvas_y][x] = '*'\n",
    "    \n",
    "    # 打印图形\n",
    "    for row in canvas:\n",
    "        print(''.join(row))\n",
    "\n",
    "# 调用函数，指定画布宽度和高度\n",
    "draw_sin_wave(width=80, height=20)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
